from flask import Flask, request, jsonify, send_file
from flask_cors import CORS, cross_origin
from HGMSimController import HGMSimController


app = Flask(__name__)
CORS(app)

hgmSimController = HGMSimController()


@app.route("/uploadSample", methods=["GET", "POST"])
def addSample():
    if request.method == "POST":
        # 检查是否有文件被上传
        if "file" not in request.files:
            return jsonify(
                {"success": False, "message": "Does not contain file attributes"}, 403
            )
    file = request.files["file"]
    userId = request.form["userId"]
    userName = request.form["userName"]
    # 如果用户没有选择文件，浏览器可能会提交一个空文件，需要检查一下
    if file.filename == "":
        return jsonify({"success": False, "message": "Empty file uploaded"}), 403
    if hgmSimController.addSample(file, userId, userName) == False:
        return jsonify({"success": False, "message": "File preprocessing failure"}), 403
    return jsonify({"success": True, "message": "File uploaded successfully"}), 200


@app.route("/querySampleInfoListByUser", methods=["GET", "POST"])
def querySampleInfoListByUser():
    if request.method == "POST":
        data = request.get_json()
        userId = data.get("userId")
        data = hgmSimController.querySampleInfoListByUser(userId)
        return jsonify(data), 200


@app.route("/queryDashBoardInfo", methods=["GET", "POST"])
def queryDashBoardInfo():
    if request.method == "POST":
        return jsonify(hgmSimController.queryDashBoardInf()), 200


@app.route("/querySampleInfo", methods=["GET", "POST"])
def querySampleInfo():
    if request.method == "POST":
        data = request.get_json()
        sampleId = data.get("sampleId")
        return hgmSimController.querySampleInfo(sampleId)


@app.route("/calculateSamplePairSim", methods=["GET", "POST"])
def calculateSamplePairSim():
    if request.method == "POST":
        data = request.get_json()
        sampleId1 = data.get("sampleId1")
        sampleId2 = data.get("sampleId2")
        res = hgmSimController.calculateSamplePairSim(sampleId1, sampleId2)
        return res


@app.route("/queryGreyImage", methods=["GET", "POST"])
def queryGreyImage():
    if request.method == "POST":
        data = request.get_json()
        sampleId = data.get("tarId")
        res = hgmSimController.queryGreyImage(sampleId)
        return send_file(res, mimetype="image/png")


@app.route("/calculateSampleSimTopK", methods=["GET", "POST"])
def calculateSampleSimTopK():
    if request.method == "POST":
        data = request.get_json()
        sampleId = data.get("sampleId1")
        return hgmSimController.calculateSampleSimTopk(sampleId)


# @app.route("/querySimHistoryList")
# def querySimHistoryList():
#     return HGMSimController.querySimHistoryList(operatorName)


# @app.route("/querySamplePairInf")
# def querySamplePairInf():
#     return HGMSimController.querySamplePairInf(sampleId1, sampleId2)


# @app.route("/delSamplePaitInf")
# def delSamplePaitInf():
#     return HGMSimController.delSample(sampleId)

if __name__ == "__main__":
    app.run(debug=True)
